class Solution(object):
    def merge(self, intervals):
        if not intervals:
            return []

        intervals = sorted(intervals, key=lambda x: x[0])
        res = [intervals[0]]

        for i in intervals:
            last= res[-1]
            if last[1] >= i[0]:
                res[-1][1] = max(last[1], i[1])
            else:
                res.append(i)
        return res
        

intervals = [[1,3],[2,6],[8,10],[15,18]]
a = Solution().merge(intervals)



